<template>
  <transition
    name="animate__animated animate__bounce"
    appear
    enter-active-class="animate__flip"
    leave-active-class="animate__hinge"
  >
    <div class="login-wrap">
      <div class="content">
        <div class="content-wrapper">
          <div class="left">
            <svg
              viewBox="40 50 100 90"
              width="100%"
              height="100%"
              class="curve"
            >
              <path
                class="path"
                stroke-width="2"
                d="M 50 100 Q 75 50 100 100 Q 125 150 150 100"
                stroke="#fff"
                fill="none"
              ></path>
            </svg>
            <img class="left-img" src="../assets/Saly.png" alt="leftImg" />
          </div>
        </div>
      </div>

      <Tablepet />

      <el-form
        :inline="true"
        class="login-container form-wrapper"
        :rules="rules"
        :model="form"
        status-icon
        label-width="70px"
        ref="form"
      >
        <h2 class="header login_title">登录</h2>
        <el-form-item label="用户名" prop="username">
          <el-input
            class="border-item"
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            type="password"
            class="border-item"
          ></el-input>
        </el-form-item>
        <el-form-item class="action">
          <el-button
            class="btn"
            @click="submit"
            style="margin-left: 100px"
            type="primary"
            @keyup.enter="submit"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </transition>
</template>

<script>
// import Mock from "mockjs";
import Cookie from "js-cookie";
import { getMenu } from "../api/index";
import Tablepet from "./Tablepet.vue";
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    //登录
    submit() {
      //token信息
      //   const token = Mock.Random.guid();
      //token信息存入cookie用于不同页面间通信
      //   Cookie.set("token", token);

      //通过校验
      this.$refs.form.validate((valid) => {
        if (valid) {
          getMenu(this.form).then(({ data }) => {
            console.log(data);
            if (data.code === 20000) {
              const newToken = data.data.token;
              //保存cookie信息
              Cookie.set("token", newToken);

              //获取菜单的数据，存入store中
              //data.data.menu;
              this.$store.commit("tab/setMenu", data.data.menu);

              //动态路由
              this.$store.commit("tab/addMenu", this.$router);

              //跳转首页
              this.$router.replace("/home");

              //提示框
              this.$message({
                type: "success",
                message: "登录成功!",
              });
            } else {
              //提示框
              this.$message({
                type: "error",
                message: "密码或用户名不正确，登录失败",
              });
              setTimeout(() => {
                this.$refs.form.resetFields();
              }, 1000);
            }
          });
        }
      });
    },
  },
  components: {
    Tablepet,
  },
};
</script>
<style lang="less" scoped>
.login-container {
  position:absolute;
  right: 350px;
  width: 350px;
  height: 400px;
  margin: 180px auto;
  padding: 35px 35px 15px 35px;
  border-radius: 15px;
  box-shadow: 0 0 25px #cac6c6;
  box-sizing: border-box;
  .el-input {
    width: 198px;
  }
  .login_title {
    text-align: center;
    margin-bottom: 40px;
    color: 505458;
  }
}

.login-wrap {
  font-family: JetBrains Mono Medium;
  display: flex;
  align-items: center;
  justify-content: center;
  .content {
    width: 80vw;
    height: 80vh;
    border-radius: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    box-sizing: border-box;
    position: relative;
    margin: 100px auto;
    background-color: rgb(249, 213, 184);
    .content-wrapper {
      width: 100%;
      height: 100%;
      background-color: rgb(243, 172, 157);
      background-image: radial-gradient(
          closest-side,
          rgb(245, 189, 214),
          rgba(235, 105, 78, 0)
        ),
        radial-gradient(closest-side, rgb(243, 172, 157), rgba(243, 11, 164, 0)),
        radial-gradient(
          closest-side,
          rgb(240, 153, 159),
          rgba(254, 234, 131, 0)
        ),
        radial-gradient(
          closest-side,
          rgb(202, 202, 202),
          rgba(170, 142, 245, 0)
        ),
        radial-gradient(
          closest-side,
          rgb(233, 114, 168),
          rgba(248, 192, 147, 0)
        );
      background-size: 130vmax 130vmax, 80vmax 80vmax, 90vmax 90vmax,
        110vmax 110vmax, 90vmax 90vmax;
      background-position: -80vmax -80vmax, 60vmax -30vmax, 10vmax 10vmax,
        -30vmax -10vmax, 50vmax 50vmax;
      background-repeat: no-repeat;
      border-radius: 30px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        width: 30%;
        height: 100%;
        border-radius: 30px;
        position: relative;
        background-image: linear-gradient(
          rgb(230, 79, 144),
          rgb(238, 113, 132),
          rgb(245, 150, 109),
          rgb(255, 174, 53)
        );
        .curve {
          position: absolute;
          .path {
            transform: skew(0deg, -20deg) translate(-10px, 20px);
          }
        }
        .left-img {
          height: 90%;
          position: absolute;
          top: 10%;
          animation-name: PAnimation;
          animation-direction: alternate;
          animation-timing-function: linear;
          animation-iteration-count: infinite;
          animation-duration: 3s;
        }
      }
    }
  }

  @keyframes PAnimation {
    0% {
      transform: translate(0%, -3%);
    }
    100% {
      transform: translate(0%, 3%);
    }
  }
}

.form-wrapper {
  width: 400px;
  background-color: #f49070;
  color: #fff;
  border-radius: 20px;
  padding: 50px;
}

.form-wrapper .btn {
  width: 60%;
  text-transform: uppercase;
  border: 2px solid #0e92b3;
  text-align: center;
  line-height: 50px;
  border-radius: 30px;
}

.form-wrapper .action .btn:hover {
  background-image: linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%);
}
</style>
